package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 一百三十二、PPVT
 */
public class Table132 extends AbstractTable implements TableCalculate {

    String code = "Cancer";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetPpvtFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return new String[0];
    }

    private ClsAnswerResult GetPpvtFactorScore(ClsAnswerRequest answer) {
        // 总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            int i;
            int intRightNum = 0; // 完成的题目中正确题数
            int intErrNum = 0; // 完成的题目中错误题数
            int intFinish = 120; // 完成的题数
            String strZhiLin = ""; // 智龄
            String strChangeAge = ""; // 改变年龄格式，以便查表
            int intIQ = 0;
            int intIQ_Percentile = 0; // 智商和百分位数

            // 正确答案
            String[] strStandardAns = {"4", "3", "1", "1", "4", "2", "2", "1", "4", "2", "3", "4", "4", "2", "2", "1",
                    "4", "1", "1", "1", "3", "3", "1", "3", "4", "1", "4", "2", "4", "2", "1", "3", "2", "3", "1", "1",
                    "4", "4", "4", "4", "1", "1", "2", "1", "3", "3", "3", "2", "1", "4", "4", "2", "2", "4", "3", "3",
                    "3", "2", "1", "2", "1", "3", "1", "1", "1", "2", "4", "2", "3", "4", "4", "2", "3", "2", "4", "4",
                    "2", "3", "2", "1", "1", "1", "1", "1", "1", "1", "1", "4", "1", "1", "3", "3", "4", "1", "1", "1",
                    "2", "3", "3", "1", "3", "3", "1", "2", "3", "1", "3", "2", "1", "3", "2", "1", "4", "4", "3", "3",
                    "3", "2", "2", "2"};

            int[] intArrRightNum = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                    51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
                    76, 77, 78, 79, 80, 81, 82, 83, 84, 85};

            String[] strArrZhiLin = {"3-3", "3-4", "3-5", "3-6", "3-7", "3-8", "3-9", "3-11", "4-0", "4-1", "4-2",
                    "4-3", "4-4", "4-5", "4-6", "4-8", "4-9", "4-10", "4-11", "5-0", "5-1", "5-2", "5-3", "5-5", "5-6",
                    "5-7", "5-8", "5-9", "5-10", "6-0", "6-1", "6-2", "6-3", "6-4", "6-5", "6-6", "6-7", "6-9", "6-10",
                    "6-11", "7-0", "7-1", "7-2", "7-3", "7-4", "7-6", "7-7", "7-8", "7-9", "7-10", "7-11", "8-0", "8-1",
                    "8-3", "8-4", "8-5"};

            String strIQ_Percentile = "";
            String strIQ = "";

            // 1正确题数
            int MAX_NUM = 120;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] strClientAnswerA = strAnswer_c[0].toString().split(g_nChar);

            if (strClientAnswerA.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            // 三个数量
            for (i = 0; i < strClientAnswerA.length; i++) {
                if (!strClientAnswerA[i].equals("0")) {
                    if (strClientAnswerA[i].equals(strStandardAns[i])) {
                        intRightNum += 1;
                    } else {
                        intErrNum += 1;
                    }
                } else {
                    intFinish = i + 1;
                }
            }

            if (intRightNum < 30) {
                strZhiLin = "小于3-3";
            } else if (intRightNum > 85) {
                strZhiLin = "大于8-5";
            } else {
                // 用个数组比对一下，然后确定智龄
                for (i = 0; i < intArrRightNum.length; i++) {
                    if (intRightNum == intArrRightNum[i]) {
                        strZhiLin = strArrZhiLin[i].toString();
                    }
                }
            }

            // 改变年龄格式
            if (answer.getAge() < 3.03) {
                strChangeAge = "3-3-3-8";
            } else if (answer.getAge() >= 3.03 && answer.getAge() <= 3.08) {
                strChangeAge = "3-3-3-8";
            } else if (answer.getAge() >= 3.09 && answer.getAge() <= 4.02) {
                strChangeAge = "3-9-4-2";
            } else if (answer.getAge() >= 4.03 && answer.getAge() <= 4.08) {
                strChangeAge = "4-3-4-8";
            } else if (answer.getAge() >= 4.09 && answer.getAge() <= 5.05) {
                strChangeAge = "4-9-5-5";
            } else if (answer.getAge() >= 5.06 && answer.getAge() <= 6.05) {
                strChangeAge = "5-6-6-5";
            } else if (answer.getAge() >= 6.06 && answer.getAge() <= 7.05) {
                strChangeAge = "6-6-7-5";
            } else if (answer.getAge() >= 7.06 && answer.getAge() <= 8.05) {
                strChangeAge = "7-6-8-5";
            } else // (answer.getAge() > 8.05)
            {
                strChangeAge = "7-6-8-5";
            }

            switch (strChangeAge) {
                case "3-3-3-8":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,5,6,9,11,17,18,21,26,28,31,34,36,40,41,45,49,53,60,64,70,73,77,81,83,85,86,88,90,92,93,94,97,98,98,99,99,99,99,99,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "47,48,49,51,53,54,56,58,59,61,63,64,66,67,69,71,72,74,76,77,79,80,82,84,85,87,89,90,92,93,95,97,98,100,102,103,105,107,108,110,111,113,115,116,118,120,121,123,124,126,128,129,131,133,134,136,137,139,141,142,144,146,147,148,149,151,152,154,155,157,159,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160";
                    break;
                case "3-9-4-2":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,3,4,4,7,8,9,11,12,16,17,19,22,27,29,33,34,38,42,45,48,54,63,68,71,74,77,81,82,85,90,91,92,92,93,93,94,95,96,96,97,97,97,97,98,98,99,99,99,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "42,43,45,46,48,49,51,52,54,55,57,58,60,61,63,64,66,67,69,70,72,73,75,76,78,79,81,82,84,85,87,88,90,91,93,94,96,97,98,100,101,103,104,106,107,109,110,112,113,115,116,118,119,121,122,124,125,127,128,130,131,133,134,136,137,139,140,142,143,145,146,148,149,151,152,154,155,157,158,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160";
                    break;
                case "4-3-4-8":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,6,7,8,9,10,11,13,14,15,20,24,26,29,36,43,47,49,53,56,59,61,66,68,75,79,85,88,89,90,91,92,93,94,95,96,96,96,96,96,97,97,97,97,97,97,98,98,99,99,99,99,99,99,99,99,99,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "41,42,43,45,46,48,49,50,52,53,55,56,57,59,60,62,63,64,66,67,69,70,71,73,74,76,77,78,80,81,83,84,86,87,88,90,91,93,94,95,97,98,100,101,102,104,105,107,108,109,111,112,114,115,116,118,119,121,122,123,125,126,128,129,131,132,133,135,136,138,139,140,142,143,145,146,147,149,150,152,153,154,156,157,159,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160";
                    break;
                case "4-9-5-5":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,4,7,7,8,9,14,18,24,24,27,29,35,39,46,50,56,59,63,67,69,73,74,75,77,80,83,84,85,87,88,88,90,91,92,92,93,94,96,96,98,98,99,99,99,99,99,99,99,99,99,99,99,99,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "32,33,34,35,37,38,40,41,42,44,45,46,48,49,51,52,53,55,56,58,59,60,62,63,65,66,67,69,70,71,73,74,76,77,79,80,81,83,84,86,87,88,90,91,93,94,95,97,98,100,101,102,104,105,107,108,109,111,112,114,115,116,118,119,121,122,123,125,126,128,129,130,132,133,135,136,137,139,140,142,143,144,146,147,149,150,151,153,154,156,157,158,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160,160";
                    break;
                case "5-6-6-5":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,3,5,5,8,11,17,19,25,26,27,30,33,35,40,46,47,48,49,51,58,59,60,63,68,69,70,73,78,81,83,84,84,85,85,87,88,89,92,92,92,92,93,94,95,95,96,97,98,98,98,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "31,32,33,34,36,37,39,40,41,43,44,45,47,48,50,51,52,53,54,55,57,58,59,60,61,63,64,65,66,67,68,70,71,72,73,74,76,77,78,79,80,81,83,84,85,86,87,88,90,91,92,93,94,96,97,98,99,100,101,103,104,105,106,107,108,110,111,112,113,114,116,117,118,119,120,121,123,124,125,126,127,129,130,131,132,133,134,136,137,138,139,140,141,143,144,145,146,147,149,150,151,152,153,154,156,157,158,159,160,160,160,160,160,160,160,160,160,160,160,160,160";
                    break;
                case "6-6-7-5":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,3,4,5,6,6,6,7,8,9,10,11,12,12,15,17,17,18,19,22,23,24,26,30,31,32,34,37,38,40,42,45,47,50,53,56,59,65,67,71,73,77,79,82,84,88,91,92,93,94,95,95,96,96,96,97,98,98,99,99,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "26,27,28,29,30,31,32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,48,49,50,51,52,54,55,56,57,58,59,60,61,62,63,64,65,67,68,69,70,71,72,73,74,75,76,77,78,80,81,82,83,84,85,86,87,88,89,90,91,93,94,95,96,97,98,99,100,101,102,103,104,105,107,108,109,110,111,112,113,114,115,116,117,118,120,121,122,123,124,125,126,127,128,129,130,131,133,134,135,136,137,138,139,140,141,142,143,144,146,147,148,149,150,151,152,153,154,155";
                case "7-6-8-5":
                    strIQ_Percentile = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,3,6,7,7,8,8,8,8,9,9,9,10,10,12,15,17,18,20,22,25,26,28,30,35,39,40,42,47,48,58,62,65,71,75,77,79,83,85,87,90,93,94,95,96,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100";
                    strIQ = "16,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,18,20,21,22,24,25,26,28,29,30,32,34,35,36,37,38,40,41,42,44,45,46,48,49,50,52,53,54,56,57,58,60,61,62,64,65,66,68,69,70,72,73,74,76,77,78,80,81,82,84,85,86,88,89,90,92,93,94,96,97,98,100,101,102,104,105,106,108,109,110,112,113,114,116,117,118,120,121,122,124,125,126,127,129,130,132,133,134,136,137,138,140,141,142,144,145,146,148,149,150";
                    break;
            }

            // 得到智商数和智商百分位
            intIQ_Percentile = Integer.valueOf(strIQ_Percentile.split(",")[intRightNum]);
            intIQ = Integer.valueOf(strIQ.split(",")[intRightNum]);

            ClsScore.Ppvt ppvt = new ClsScore.Ppvt();

            ppvt.setRight_num_o(intRightNum);
            ppvt.setErr_num_o(intErrNum);
            ppvt.setFinish_num_o(intFinish);
            ppvt.setIq_o(intIQ);
            ppvt.setIq_percentile_o(intIQ_Percentile);
            ppvt.setAge_of_wisdom_a(strZhiLin);

            result.setObject(ppvt);
            return result;

        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }

    }
    
 // 一百三十二、PPVT
 	private String[] GetPpvtReport(Map map) {
 		// 下面因子名称字段改成.toLowerCase()小写，是因为JAVA转json的时候会把大写字母转成小写字母
 		// 第一个，错误信息；第二个，量表得分部分；第三个，量表指导意见_d数组部分
 		// String[] strResult = {"","",""};
 		String[] strResult = { "", "", "", "", "" };

 		try {
 			int i = 0;
 			String[] gcName = "初分,智商".split(",");
 			String[] geName = "Right_num,Iq".toLowerCase().split(",");

 			if (g_inblFactor) // 因子得分
 			{

 				strResult[1] += "<table cellpadding='0' cellspacing='0' width='100%' border='0'>";

 				for (i = 0; i < gcName.length; i++) {
 					strResult[1] += "<tr><td" + subTitle(0, tWidth * 2) + ">" + gcName[i] + "</td>" + "<td"
 							+ subValue(0, -1) + ">" + map.get(geName[i] + "_o") + "</td></tr>";
 				}

 				strResult[1] += "<tr><td" + subTitle(0, tWidth * 2) + ">" + "智商百分位" + "</td>" + "<td" + subValue(0, -1)
 						+ ">" + map.get("iq_percentile" + "_o") + "%" + "</td></tr>";

 				strResult[1] += "<tr><td" + subTitle(0, tWidth * 2) + ">" + "智龄" + "</td>" + "<td" + subValue(0, -1)
 						+ ">" + map.get("age_of_wisdom" + "_a") + "</td></tr>";

 				strResult[1] += "</table>";
 			}

 			if (Integer.valueOf(map.get("iq_percentile_o").toString()) < 5) {
 				strResult[3] = "*";
 				strResult[4] = "智力百分等级异常";
 			} else {
 				strResult[3] = "0";
 				strResult[4] = "正常";
 			}

 			return strResult;
 		} catch (ArithmeticException e) {
 			strResult[0] = "失败";
 			return strResult;
 		}

 	}



}
